package com.study.talk.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.bumptech.glide.Glide;
import com.study.talk.app.base.BaseActivity;
import com.study.talk.app.other.ImagePreviewActivity;
import com.study.talk.app.friend.UserInfoActivity;
import com.study.talk.app.self.UserInfoSelfActivity;
import com.study.talk.databinding.MsgLayoutLBinding;
import com.study.talk.databinding.MsgLayoutRBinding;
import com.study.talk.entity.MessageRecord;
import com.study.talk.net.OkHttp;
import com.study.talk.util.GsonInstance;
import com.study.talk.util.TimeAgoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author thor
 * @description: 聊天页面的列表适配器
 * @date 2024/7/8 9:32
 */
public class ChatMessageListAdapter extends RecyclerView.Adapter<ChatMessageListAdapter.ViewHolder> {

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public MsgLayoutLBinding msgLayoutLBinding;
        public MsgLayoutRBinding msgLayoutRBinding;
        public Boolean isSelf;

        public ViewHolder(ViewBinding viewBinding, Boolean isSelf) {
            super(viewBinding.getRoot());
            this.isSelf = isSelf;
            if (isSelf) msgLayoutRBinding = (MsgLayoutRBinding) viewBinding;
            else msgLayoutLBinding = (MsgLayoutLBinding) viewBinding;
        }
    }

    List<MessageRecord> messageRecordList;
    BaseActivity activity;


    public ChatMessageListAdapter(List<MessageRecord> messageRecordList, BaseActivity activity) {
        this.messageRecordList = messageRecordList;
        this.activity = activity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        boolean isSelf = messageRecordList.get(viewType).getSelf();
        if (isSelf) {
            return new ViewHolder(MsgLayoutRBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false), true);
        } else {
            return new ViewHolder(MsgLayoutLBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false), false);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MessageRecord messageRecord = messageRecordList.get(position);
        if (holder.isSelf) {
            holder.msgLayoutRBinding.msgContentTime.setText(TimeAgoUtils.format(messageRecord.getDateTime()));
            Glide.with(activity).load(OkHttp.URL + messageRecord.getAvatar()).into(holder.msgLayoutRBinding.msgContentHeadImg);
            holder.msgLayoutRBinding.msgContentUsername.setText("");
            if (messageRecord.getMessageType() == 0) {
                /*if (position == messageRecordList.size() - 1) {
                    if (messageRecord.getSelf()) {
                        holder.msgLayoutRBinding.msgContentTextView.setAnimation(AnimationUtils.loadAnimation(activity, R.anim.chat_right_enter));
                    } else {
                        holder.msgLayoutLBinding.msgContentTextView.setAnimation(AnimationUtils.loadAnimation(activity, R.anim.chat_left_enter));
                    }
                }*/
                holder.msgLayoutRBinding.msgContentTextView.setText(messageRecord.getContent());
            } else {
                holder.msgLayoutRBinding.msgContentTextView.setVisibility(View.GONE);
                holder.msgLayoutRBinding.msgContentImageView.setVisibility(View.VISIBLE);
                holder.msgLayoutRBinding.msgContentImageView.setOnClickListener(v -> {
                    List<String> imageList = new ArrayList<>();
                    for (MessageRecord record : messageRecordList) {
                        if (record.getMessageType() == 1) {
                            imageList.add(record.getContent());
                        }
                    }
                    Intent intent = new Intent(activity, ImagePreviewActivity.class);
                    intent.putExtra("imageList", GsonInstance.instance.toJson(imageList));
                    intent.putExtra("index", position);
                    activity.startActivity(intent);
                });
                Glide.with(activity).load(OkHttp.URL + messageRecord.getContent()).into(holder.msgLayoutRBinding.msgContentImageView);
            }
            holder.msgLayoutRBinding.msgContentHeadImg.setOnClickListener(view1 -> {
                Intent intent;
                if (messageRecord.getSelf()) {
                    intent = new Intent(activity, UserInfoSelfActivity.class);
                } else {
                    intent = new Intent(activity, UserInfoActivity.class);
                }
                intent.putExtra("userId", messageRecord.getUserId());
                activity.startActivity(intent);
            });
        } else {
            holder.msgLayoutLBinding.msgContentTime.setText(TimeAgoUtils.format(messageRecord.getDateTime()));
            Glide.with(activity).load(OkHttp.URL + messageRecord.getAvatar()).into(holder.msgLayoutLBinding.msgContentHeadImg);
            holder.msgLayoutLBinding.msgContentUsername.setText("");

            if (messageRecord.getMessageType() == 0) {
                /*if (position == messageRecordList.size() - 1) {
                    if (messageRecord.getSelf()) {
                        holder.msgLayoutRBinding.msgContentTextView.setAnimation(AnimationUtils.loadAnimation(activity, R.anim.chat_right_enter));
                    } else {
                        holder.msgLayoutLBinding.msgContentTextView.setAnimation(AnimationUtils.loadAnimation(activity, R.anim.chat_left_enter));
                    }
                }*/
                holder.msgLayoutLBinding.msgContentTextView.setText(messageRecord.getContent());
            } else {
                holder.msgLayoutLBinding.msgContentTextView.setVisibility(View.GONE);
                holder.msgLayoutLBinding.msgContentImageView.setVisibility(View.VISIBLE);
                holder.msgLayoutLBinding.msgContentImageView.setOnClickListener(v -> {
                    List<String> imageList = new ArrayList<>();
                    for (MessageRecord record : messageRecordList) {
                        if (record.getMessageType() == 1) {
                            imageList.add(record.getContent());
                        }
                    }
                    Intent intent = new Intent(activity, ImagePreviewActivity.class);
                    intent.putExtra("imageList", GsonInstance.instance.toJson(imageList));
                    intent.putExtra("index", position);
                    activity.startActivity(intent);
                });
                Glide.with(activity).load(OkHttp.URL + messageRecord.getContent()).into(holder.msgLayoutLBinding.msgContentImageView);
            }
            holder.msgLayoutLBinding.msgContentHeadImg.setOnClickListener(view1 -> {
                Intent intent;
                if (messageRecord.getSelf()) {
                    intent = new Intent(activity, UserInfoSelfActivity.class);
                } else {
                    intent = new Intent(activity, UserInfoActivity.class);
                }
                intent.putExtra("userId", messageRecord.getUserId());
                activity.startActivity(intent);
            });

        }

    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return messageRecordList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }
}
